//
//  XCCollectionCellForFind.m
//  TTLestening
//
//  Created by dllo on 16/3/21.
//  Copyright © 2016年 dilo. All rights reserved.
//

#import "XCCollectionCellForFind.h"
#import "NetworkingHandlerOfBlock.h"
#import "XCModelForFind.h"
#import "XCCellForScroller.h"
#import "XTTableViewCell.h"
#import "XTCellForeveryone.h"
#import "XCCellofgedna.h"

#import "XTCellOfaiqing.h"
#import "XTCellOfgeshou.h"
#import "XTCellOfxinge.h"
#import "XTCellOfMV.h"

#import "XCModelForHeader.h"
#import "XTCellOfdujia.h"
#import "XTCellOfyazhou.h"
#define WIDGHT  self.contentView.frame.size.width
#define HEIGHT  self.contentView.frame.size.height

#define WIDGHTT [UIScreen mainScreen].bounds.size.width / 375
#define HEIGHTT [UIScreen mainScreen].bounds.size.height / 667



@interface XCCollectionCellForFind ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *arr;
@property (nonatomic, retain) NSMutableArray  *arrForHeader;
@end


@implementation XCCollectionCellForFind

- (instancetype)initWithFrame:(CGRect)frame {
    
    
    self = [super initWithFrame:frame];
    
    if (self) {
       
        [self handleData];
        [self createTableView];
        
    }
    return self;
    

}

- (void)handleData {

    NSString *url = @"http://api.dongting.com/frontpage/frontpage?location=0&version=1458562426544&app=ttpod&v=v8.1.5.2016022418&user_id=0&mid=iPhone5C&f=f320&s=s310&imsi=&hid=&splus=9.2&active=1&net=2&openudid=71ae591d23e3955faffe1f13002174549b820ffe&idfa=766C509A-D4C4-4D11-89EA-A316DCF2B7CC&utdid=Vo%2FO9IPtv1UDAHSbJQCXwF%2F6&alf=201200&bundle_id=com.ttpod.music&latitude=38.88245&longtitude=121.5396";
    
   [NetworkingHandler networkingWithUrl:url completeHandle:^(NSURLResponse *response, NSData *data, id result) {
      
       self.arr = [NSMutableArray array];
       self.arrForHeader = [NSMutableArray array];
       NSArray *dataArr= [result objectForKey:@"data"];
       
       for (NSDictionary *dic in dataArr) {
           
           XCModelForHeader *modelForHeader = [[XCModelForHeader alloc] init];
           [modelForHeader setValuesForKeysWithDictionary:dic];
           [self.arrForHeader addObject:modelForHeader];
           
           NSArray *arrData = [dic objectForKey:@"data"];
           NSMutableArray *arrayIN = [NSMutableArray array];
           
           for (NSDictionary *dicData in arrData) {
               
               XCModelForFind *model = [[XCModelForFind alloc] init];
               
               [model setValuesForKeysWithDictionary:dicData];
               
               [arrayIN addObject:model];
              
               
           }
        
           [self.arr addObject:arrayIN];
           
       }
       
       
       [self.tableView reloadData];
       
   }];
 
}


- (void)createTableView {

//    self.imageArr = @[@"placehodel2", @"placehodel"];
    
    self.tableView = [[UITableView alloc] initWithFrame:self.contentView.frame style:UITableViewStylePlain];
    [self.contentView addSubview:self.tableView];
    
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    
    // 轮播图
    [self.tableView registerNib:[UINib nibWithNibName:@"XCCellForScroller" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"poolForScroller"];
    
    // 第二个分区的 cell
    UINib *nibCell = [UINib nibWithNibName:@"XTTableViewCell" bundle:[NSBundle mainBundle]];
    
    [self.tableView registerNib:nibCell forCellReuseIdentifier:@"poolForXTTab"];

    // 大家都在听
    [self.tableView registerClass:[XTCellForeveryone class] forCellReuseIdentifier:@"poolForEveryone"];
    
    
   // [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"pool3"];
    //歌单
    [self.tableView registerClass:[XCCellofgedna class] forCellReuseIdentifier:@"poolForgedan"];
    //爱情
    [self.tableView registerClass:[XTCellOfaiqing class] forCellReuseIdentifier:@"poolForaiqing"];
    //歌手
    [self.tableView registerClass:[XTCellOfgeshou class] forCellReuseIdentifier:@"poolForgeshou"];
     //新歌新碟
    [self.tableView registerClass:[XTCellOfxinge class] forCellReuseIdentifier:@"poolForxinge"];
    //MV
    [self.tableView registerClass:[XTCellOfMV class] forCellReuseIdentifier:@"poolForMV"];
    //独家
    
    [self.tableView registerClass:[XTCellOfdujia class] forCellReuseIdentifier:@"poolFordujia"];
    //亚洲新歌
    [self.tableView registerClass:[XTCellOfyazhou class] forCellReuseIdentifier:@"poolForyazhou"];
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return self.arr.count;

}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    
    return 1;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
 
    if (indexPath.section == 0) {
        
        
        XCCellForScroller *cell = [tableView dequeueReusableCellWithIdentifier:@"poolForScroller"];
        
        
        [cell passModel:[self.arr objectAtIndex:0]];
        
        
        return cell;
        
        
    } else if (indexPath.section == 1) {
    
        XTTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"poolForXTTab"];
        
       
        [cell passModel:[self.arr objectAtIndex:1]];
        
        return cell;
    
    
    } else if (indexPath.section == 2) {
    
        XTCellForeveryone *cell = [tableView dequeueReusableCellWithIdentifier:@"poolForEveryone"];
        
        
        [cell passModel:[self.arr objectAtIndex:2]];
                
        return cell;
    
    
    
    }else if (indexPath.section == 3){
    
        XCCellofgedna *cell = [tableView dequeueReusableCellWithIdentifier:@"poolForgedan"];
        
        XCModelForHeader *model = [self.arrForHeader objectAtIndex:3];
        [cell passModelForArray:[self.arr objectAtIndex:3]  headerModel:model];
        
        return cell;
    
    }else if (indexPath.section == 4){
    
    
        XTCellOfaiqing *cell = [tableView dequeueReusableCellWithIdentifier:@"poolForaiqing"];
    
        XCModelForHeader *model = [self.arrForHeader objectAtIndex:4];
        [cell passModelForArray:[self.arr objectAtIndex:4] headerModel:model];
        
        return cell;
        
    }else if (indexPath.section == 5){
        
        XTCellOfgeshou *cell = [tableView dequeueReusableCellWithIdentifier:@"poolForgeshou"];
        
        XCModelForHeader *model = [self.arrForHeader objectAtIndex:5];
        [cell passModelForArray:[self.arr objectAtIndex:5] headerModel:model];
        
        return cell;
        
    }else if (indexPath.section == 6){
        
        XTCellOfxinge *cell = [tableView dequeueReusableCellWithIdentifier:@"poolForxinge"];
        
        XCModelForHeader *model = [self.arrForHeader objectAtIndex:6];
        [cell passModelForArray:[self.arr objectAtIndex:6] headerModel:model];

        
        return cell;
    }else if (indexPath.section == 7){
        
        XTCellOfMV *cell = [tableView dequeueReusableCellWithIdentifier:@"poolForMV"];
        
        
        XCModelForHeader *model = [self.arrForHeader objectAtIndex:7];
        
        [cell passModelForArray:[self.arr objectAtIndex:7] headerModel:model];

        
        return cell;
        
    }else if (indexPath.section == 8){
        XTCellOfdujia *cell = [tableView dequeueReusableCellWithIdentifier:@"poolFordujia"];
        
        XCModelForHeader *model = [self.arrForHeader objectAtIndex:8];
        
        [cell passModelForArray:[self.arr objectAtIndex:8] headerModel:model];
        
        return cell;
     
    }else{
    
        XTCellOfyazhou *cell = [tableView dequeueReusableCellWithIdentifier:@"poolForyazhou"];
        XCModelForHeader *model = [self.arrForHeader objectAtIndex:9];
        
        [cell passModelForArray:[self.arr objectAtIndex:9] headerModel:model];
        
        
        return cell;
    }
        
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

     CGFloat height = [UIScreen mainScreen].bounds.size.height;
    CGFloat weigh = [UIScreen mainScreen].bounds.size.width;
    
    if (indexPath.section == 0) {
    
        return weigh * 0.5;
    
    } else if (indexPath.section == 1) {
    
        return weigh / 4;
    
    }
    else if (indexPath.section == 2)  {
    
        return height / 10 * 3 + 50;
    
    }else if (indexPath.section == 3){
    

        return weigh * 0.85 + 40 * HEIGHTT ;
        
    }else if (indexPath.section == 4 || indexPath.section == 6 || indexPath.section == 7){
        
         return weigh * 0.425 + 40 * HEIGHTT;
    
    }else if(indexPath.section == 5 || indexPath.section == 8){
        
        return  weigh * 0.75 + HEIGHTT * 40;
       
    }else{
        
        
        return weigh * 0.25 + 40 * HEIGHTT ;
    }


}



@end
